package fun.coding.leetcode;

public class ExcelSheetColumnTitle {

	public static void main(String[] args) {
		ExcelSheetColumnTitle ins = new ExcelSheetColumnTitle();
		System.out.println(ins.convertToTitle(52));
		//System.out.println(ins.convertToTitle(1000000001));
	}
	/**
	 *  1 -> A
	    2 -> B
	    3 -> C
	    ...
	    26 -> Z
	    27 -> AA
	    28 -> AB 
	 */
	public String convertToTitle(int n) {
		if (n <= 0) return null;
		
		StringBuilder res = new StringBuilder();
		
		int num = n;
		
		// Basically convert this to base 26
		while (num > 0) {
			res.append(numberToCapLetter(num % 26));
			num = (num - 1) / 26;
		}
		
		return res.reverse().toString();
	}
	
	public String numberToCapLetter(int num) {
		if (num == 0) return "Z";
		char c = 'A';
		char res = (char)(c + num - 1);
		return Character.toString(res);
	}
}
